const emilecharc=document.querySelector('.menu-item')
const mobilephone=emilecharc.nextElementSibling

const inputemail=document.querySelector('.l-form-pass-email')

const inputmobile=document.querySelector('.l-form-pass-mobile')

mobilephone.addEventListener('click',function(){
    const menuitemactive=document.querySelector('.menu-item-active')
    menuitemactive.classList.remove('menu-item-active')
    mobilephone.classList.add('menu-item-active')
    inputemail.style.zIndex=0;
    inputmobile.style.zIndex=1;
})
emilecharc.addEventListener('click',function(){
    const menuitemactive=document.querySelector('.menu-item-active')
    menuitemactive.classList.remove('menu-item-active')
    emilecharc.classList.add('menu-item-active')
    inputemail.style.zIndex=1;
    inputmobile.style.zIndex=0;
})
const submit=document.querySelector('.zhuce')
const regusename=/^[a-zA-Z0-9]{6,16}$/
const regpassword=/^[a-zA-Z0-9]{6,16}$/
const regemail=/^\w+@\w+\.\w+$/
const regmobile=/^1[3-9]\d{9}$/
const form=document.querySelector('.form')





form.addEventListener('submit', function(e){
    e.preventDefault();  
const usename=document.querySelector('input[name="username"]')
const password=document.querySelector('input[name="password"]')
const password1=document.querySelector('input[name="password1"]')
const mobile=document.querySelector('input[name="mobile"]')
const email=document.querySelector('input[name="email"]')
const passwordspan=password.nextElementSibling;
const usenamespan=usename.nextElementSibling
const password1span=password1.nextElementSibling
const emailspan=email.nextElementSibling
const mobilespan=mobile.nextElementSibling
    let valid=true;
    let uv=usename.value;
    let pv=password.value;
    let pv1=password1.value;
    let mv=mobile.value;
    let ev=email.value;   
    
    if(!regusename.test(uv)){
    usenamespan.innerHTML='用户名输入不正确'
      valid=false;
      
    }
    else{
        usenamespan.innerHTML=''
    }
    if(!regpassword.test(pv)){
        passwordspan.innerHTML='密码由6-16位大小写字母数字组成'
        valid=false;
    }
    else {
       passwordspan.innerHTML=''
    }
    if(pv!=pv1){
        password1span.innerHTML='密码输入不一致'
        valid=false;
    }
    else{
        password1span.innerHTML=''
    }
    if(!regemail.test(ev)&&inputemail.style.zIndex>inputmobile.style.zIndex){
    emailspan.innerHTML='注册邮箱格式有误'
        valid=false;
    }
    else if(inputemail.style.zIndex>inputmobile.style.zIndex&&regemail.test(ev)){
        emailspan.innerHTML=''
    }
    if(!regmobile.test(mv)&&inputemail.style.zIndex<inputmobile.style.zIndex){
     mobilespan.innerHTML='手机号格式有误'
        valid=false;
    }
    else if(inputemail.style.zIndex<inputmobile.style.zIndex&&regmobile.test(mv)){
        mobilespan.innerHTML=''
    }
   
  
    
    

        
    
 
    if(valid){
    
   axios({
    url: 'http://127.0.0.1:4523/m1/6252606-5946561-default/registerlogin',
    method:'post',
    data:{
        username:uv,
        password:pv,
        // email:ev,
        // mobile:mv
         email: ev,
       mobile: inputmobile.style.zIndex > inputemail.style.zIndex ? mv : ''
    }
   })
   .then(result=>{
    alert('注册成功')
    console.log(result);
    
   }).catch (error=>{
    console.log(error)
    
    })


  
    
//  })

        }
 
}

    
    // if(valid){
    //     let users={
    //         username:uv,
    //         password:pv,
    //         email:ev,
    //         mobile:mv
    //     }
    //  localStorage.setItem('users',JSON.stringify(users))
    //    alert('注册成功')
    //    location.href=''
    // }
)




